package com.gtq.uhome.controller;


import com.gtq.uhome.result.Result;
import com.gtq.uhome.service.CategoryService;
import com.gtq.uhome.vo.CategoryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


/**
 * 商品品类管理控制器
 */
@Controller
@CrossOrigin
@RequestMapping("/category")
@ResponseBody
@Api(tags = "总后台，商品品类管理接口说明文档，田小凡")
public class CategoryController {

    @Resource
    CategoryService categoryService = null;

    //查询所有
    @GetMapping
    @ApiOperation(value = "商品品类管理，查询所有",httpMethod = "GET")
    public Result<List<CategoryVo>> findAll(){
        List<CategoryVo> all = categoryService.findAll();
        return Result.ok(all);
    }

    //模糊查询
    @PostMapping("/findLike")
    @ApiOperation(value = "商品品类管理，根据名称模糊查询",httpMethod = "POST")
    @ApiImplicitParam(name = "threeName",value = "三级分类名称",dataType = "String",required = true)
    public Result<List<CategoryVo>> findLike(@RequestBody CategoryVo categoryVo){
        List<CategoryVo> like = categoryService.findLike(categoryVo);
        return Result.ok(like);
    }

    //添加商品
    @PostMapping
    @ApiOperation(value = "商品品类管理，添加商品品类",httpMethod = "POST")
    @ApiImplicitParam(name = "categoryVo",value = "商品对象，threeName-三级分类名称，twoId-二级分类id，categoryNumber-商品数量",required = true)
    public Result insertCategory(@RequestBody CategoryVo categoryVo){
        System.out.println(categoryVo);
        Boolean isInsert = categoryService.insertCategory(categoryVo);
        if(isInsert){
            return Result.ok("添加成功！");
        }else {
            return Result.failed("添加失败！");
        }
    }

    //修改商品
    @PostMapping("/updateCategory")
    @ApiOperation(value = "商品品类管理，修改商品数量",httpMethod = "POST")
    @ApiImplicitParam(name = "threeId",value = "三级分类id",dataType = "int",required = true)
    public Result updateCategory(@RequestBody CategoryVo categoryVo){
        System.out.println(categoryVo.getThreeId()+"-----------------");
        Boolean isUpdate = categoryService.updateCategory(categoryVo);
        if(isUpdate){
            return Result.ok("修改成功！");
        }else {
            return Result.failed("修改失败！");
        }
    }

    //删除商品,单个或者批量
    @PostMapping("/deleteCategory")
    @ApiOperation(value = "商品品类管理，删除商品数量",httpMethod = "POST")
    @ApiImplicitParam(name = "threeIds",value = "三级分类id集合",dataType = "int",required = true)
    public Result deleteCategory(@RequestBody List<CategoryVo> threeId){

        Boolean isDelete = categoryService.deleteCategory(threeId);
        if(isDelete){
            return Result.ok("删除成功！");
        }else {
            return Result.failed("删除失败！");
        }
    }


}